package com.ruoyi.web.controller.distribution;

import cn.distribution.groupManager.service.GroupManagerService;
import cn.distribution.messagesManager.entity.vo.MessagesManagerVO;
import cn.distribution.messagesManager.service.MessagesManagerService;
import cn.distribution.messagesManager.entity.dao.MessagesManagerDAO;
import cn.distribution.personManager.entity.dao.PersonManagerDAO;
import cn.distribution.personManager.service.PersonManagerService;
import cn.distribution.receiptManager.entity.dao.ReceiptDAO;
import cn.distribution.receiptManager.service.ReceiptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.TabableView;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhengminjie
 */
@RestController
@RequestMapping("/distribution/messagesManager")
public class MessagesManagerController extends BaseController {
    protected final Logger logger = LoggerFactory.getLogger(GroupManagerController.class);

    @Autowired
    private MessagesManagerService messagesManagerService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private PersonManagerService personManagerService;

    @Autowired
    private GroupManagerService groupManagerService;

    @Autowired
    private ReceiptService receiptService;

    @GetMapping("/count")
    public Long countMessages(Long messageType){
        String userName = SecurityUtils.getLoginUser().getUser().getUserName();
        long count =this.messagesManagerService.getCount(messageType,userName);
        return count;
    }

    @GetMapping("/join")
    public String joinMessages(Long messageType){
        String userName =SecurityUtils.getLoginUser().getUser().getUserName();
        MessagesManagerDAO messagesManagerDAO = this.messagesManagerService.getContent(messageType,userName);
        List<String> content = Arrays.asList(messagesManagerDAO.getContent().split(","));
        String name = iSysUserService.selectUserById(Long.valueOf(content.get(1))).getUserName();
        String value = content.get(0) + "," + name + "," + content.get(2) + "," +messagesManagerDAO.getId();
        return value;
    }

    @GetMapping("/get")
    public String getMessages(Long messageType){
        String userName =SecurityUtils.getLoginUser().getUser().getUserName();
        MessagesManagerDAO messagesManagerDAO = this.messagesManagerService.getContent(messageType,userName);
        messagesManagerDAO.setState(true);
        this.messagesManagerService.updateById(messagesManagerDAO);
        return messagesManagerDAO.getContent();
    }

    @GetMapping("/getdoc")
    public List<String> getdoc(){
        String userName =SecurityUtils.getLoginUser().getUser().getUserName();
        MessagesManagerDAO messagesManagerDAO = this.messagesManagerService.getContent(0L,userName);
        List<String> content = new LinkedList<>();
        content.add(messagesManagerDAO.getContent());
        content.add(messagesManagerDAO.getId().toString());
        return content;
    }

    @GetMapping("/agree")
    public AjaxResult agree(){
        String userName =SecurityUtils.getLoginUser().getUser().getUserName();
        MessagesManagerDAO messagesManagerDAO = this.messagesManagerService.getContent(0L,userName);
        messagesManagerDAO.setState(true);
        this.messagesManagerService.updateById(messagesManagerDAO);
        ReceiptDAO receiptDAO =this.receiptService.getById(messagesManagerDAO.getReceiptId());
        receiptDAO.setState("同意");
        this.receiptService.updateById(receiptDAO);
        return AjaxResult.success();
    }

    @GetMapping("/disagree")
    public AjaxResult disagree(){
        String userName =SecurityUtils.getLoginUser().getUser().getUserName();
        MessagesManagerDAO messagesManagerDAO = this.messagesManagerService.getContent(0L,userName);
        messagesManagerDAO.setState(true);
        this.messagesManagerService.updateById(messagesManagerDAO);
        ReceiptDAO receiptDAO =this.receiptService.getById(messagesManagerDAO.getReceiptId());
        receiptDAO.setState("拒绝");
        this.receiptService.updateById(receiptDAO);
        return AjaxResult.success();
    }

    @PostMapping("/add")
    public AjaxResult addGroup(@RequestBody MessagesManagerVO messagesManagerVO){
        // 将旧消息的状态更新为1 即已读
        MessagesManagerDAO messagesManagerDAO = new MessagesManagerDAO();
        messagesManagerDAO.setState(true);
        messagesManagerDAO.setId(messagesManagerVO.getId());
        this.messagesManagerService.updateById(messagesManagerDAO);
        // 将成员表格添加数据
        PersonManagerDAO personManagerDAO = new PersonManagerDAO();
        personManagerDAO.setGroupId(this.groupManagerService.getIdByNum(Long.parseLong(messagesManagerVO.getContent())));
        personManagerDAO.setGroupNum(Long.parseLong(messagesManagerVO.getContent()));
        personManagerDAO.setUserId(iSysUserService.selectUserByUserName(messagesManagerVO.getUserName()).getUserId());
        personManagerDAO.setUserIdentity("普通人");
        personManagerDAO.setDataDistribution(false);
        this.personManagerService.save(personManagerDAO);
        // 添加一条消息
        MessagesManagerDAO messagesManagerDAO1 = new MessagesManagerDAO();
        messagesManagerDAO1.setUserName(messagesManagerVO.getUserName());
        messagesManagerDAO1.setMessageType(Long.parseLong("2"));
        messagesManagerDAO1.setMessageContent(messagesManagerVO.getMessageContent());
        messagesManagerDAO1.setContent("恭喜你加入"+this.groupManagerService.getNameByNum(Long.parseLong(messagesManagerVO.getContent()))+"群");
        messagesManagerDAO1.setState(false);
        messagesManagerDAO1.setCreateTime(new Date());
        this.messagesManagerService.save(messagesManagerDAO1);
        return AjaxResult.success("成功");
    }

    @PostMapping("/refuse")
    public AjaxResult refuseGroup(@RequestBody MessagesManagerVO messagesManagerVO){
        // 将旧消息的状态更新为1 即已读
        MessagesManagerDAO messagesManagerDAO = new MessagesManagerDAO();
        messagesManagerDAO.setState(true);
        messagesManagerDAO.setId(messagesManagerVO.getId());
        this.messagesManagerService.updateById(messagesManagerDAO);
        // 添加一条消息
        MessagesManagerDAO messagesManagerDAO1 = new MessagesManagerDAO();
        messagesManagerDAO1.setUserName(messagesManagerVO.getUserName());
        messagesManagerDAO1.setMessageType(Long.parseLong("3"));
        messagesManagerDAO1.setMessageContent(messagesManagerVO.getMessageContent());
        messagesManagerDAO1.setContent("很可惜你与"+this.groupManagerService.getNameByNum(Long.parseLong(messagesManagerVO.getContent()))+"群无缘");
        messagesManagerDAO1.setState(false);
        messagesManagerDAO1.setCreateTime(new Date());
        this.messagesManagerService.save(messagesManagerDAO1);
        return AjaxResult.success("成功");
    }
}
